﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.PayModules
{
    public class PayUpdaterRegister
    {
        public NormalDataUpdater<PayModel> GetPayUpdater()
        {
            NormalDataUpdater<PayModel> systemDataUpdater = new NormalDataUpdater<PayModel>();
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataUpdating += OrderAndCustomerExist;
            systemDataUpdater.DataUpdating += AccounterExist;
            systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        private void CheckAvaliable(object sender, DataValidationEventArgs<PayModel> e)
        {
            if (e.Delete.State!=Definition.STATE_NORMAL)
            {
                e.Cancle = true;
                e.Message = String.Format("[{0}]正在生效，不能删除", e.Delete.AutoId);
            }

        }

        private void SingletonValid(object sender, DataValidationEventArgs<PayModel> e)
        {
            PayFetcher payFetcher = new PayFetcher();
            string message;
            PayModel payModel = payFetcher.GetByCode(e.Insert.Site,e.Insert.Code,out message);
            if(payModel!=null&&payModel.AutoId!=e.Insert.AutoId&& payModel.Code==e.Insert.Code)
            {
                message = String.Format("代号[{0}]已被占用", e.Insert.Code);
            }
        }
        private void StateValid(object sender, DataValidationEventArgs<PayModel> e)
        {
            if (e.Insert.State != Definition.STATE_NORMAL
                && (
                e.Insert.PropertyUpdated.Contains("OrderId") ||
                e.Insert.PropertyUpdated.Contains("Date") ||
                e.Insert.PropertyUpdated.Contains("Amount") ||
                e.Insert.PropertyUpdated.Contains("CustomerId") ||
                e.Insert.PropertyUpdated.Contains("Coupon") ||
                e.Insert.PropertyUpdated.Contains("PayType") ||
                e.Insert.PropertyUpdated.Contains("Code") ||

                e.Insert.PropertyUpdated.Contains("Accounter")
                ))
            {
                e.Cancle = true;
                e.Message = "已提审/审核的单据不允许进行修改";
                return;
            }
        }
        private void DataNotSpaceValid(object sender, DataValidationEventArgs<PayModel> e)
        {
            if (e.Insert.Amount == 0)
            {
                e.Cancle = true;
                e.Message = "不允许输入0元";
                return;
            }
            if(e.Insert.PayType == Definition.PAYTYPE_COUPON&&String.IsNullOrWhiteSpace( e.Insert.Coupon))
            {
                e.Cancle = true;
                e.Message = String.Format("支付方式为优惠券，则需要输入券号");
                return;
            }
            FiscalPeriodFetcher fiscalPeriodFetcher = new FiscalPeriodFetcher();
            if(fiscalPeriodFetcher.DateInPeriod(e.Insert.Site,e.Insert.Date))
            {
                e.Message = String.Format("本单据不在当前财务区间内，无法进行操作");
                e.Cancle = true;
                return;
            }
        }
        private void AccounterExist(object sender,DataValidationEventArgs<PayModel> e)
        {
            EmployeeFetcher employeeFetcher = new EmployeeFetcher();
            string message;
            if(employeeFetcher.Get(e.Insert.Site,e.Insert.Accounter,out message)==null)
            {
                e.Message = "制单人不能为空";
                e.Cancle = true;
                return;
            }
        }
        private void  OrderAndCustomerExist(object sender,DataValidationEventArgs<PayModel> e)
        {
            string message;
            if(e.Insert.OrderId!=0)
            {
                OrderFetcher orderFetcher = new OrderFetcher();
                OrderModel orderModel = orderFetcher.Get(e.Insert.Site, e.Insert.OrderId, out message);
                if (orderModel == null|| orderModel.State==Definition.STATE_NORMAL||orderModel.State == Definition.STATE_READY)
                {
                    e.Cancle = true;
                    e.Message = "订单不存在或状态不正确";
                    return;
                }
            }
            CustomerFetcher customerFetcher = new CustomerFetcher();
            CustomerModel customer = customerFetcher.Get(e.Insert.Site,e.Insert.CustomerId,out message);
            if(customer==null)
            {
                e.Cancle = true;
                e.Message = "客户不存在";
                return;
            }
            if (e.Insert.State == Definition.STATE_APPROVED)
            {
                decimal amountDelete = 0;
                if (e.Delete != null)
                    amountDelete = e.Delete.Amount;
                if (customer.Receiveable < customer.Received + e.Insert.Amount - amountDelete)
                {
                    e.Cancle = true;
                    e.Message = "本次收款已经超过应收金额";
                    return;
                }
            }
        }
       
    }
}
